﻿Imports System
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Design

Namespace System.Workflow.ComponentModel.Compiler
    Public Class CompositeActivityValidator
        Inherits ActivityValidator
        ' Methods
        Public Overrides Function Validate(ByVal manager As ValidationManager, ByVal obj As Object) As ValidationErrorCollection
            Dim activity As CompositeActivity = [TryCast](obj,CompositeActivity)
            If (activity Is Nothing) Then
                Throw New ArgumentException(SR.GetString("Error_UnexpectedArgumentType", New Object() { GetType(CompositeActivity).FullName }), "obj")
            End If
            If Helpers.IsActivityLocked(activity) Then
                Return New ValidationErrorCollection
            End If
            Dim errors As ValidationErrorCollection = MyBase.Validate(manager, obj)
            Dim num As Integer = 0
            Dim num2 As Integer = 0
            Dim num3 As Integer = 0
            Dim activity2 As Activity
            For Each activity2 In DirectCast(activity, ISupportAlternateFlow).AlternateFlowActivities
                num = (num + If(TypeOf activity2 Is CancellationHandlerActivity, 1, 0))
                num2 = (num2 + If(TypeOf activity2 Is FaultHandlersActivity, 1, 0))
                num3 = (num3 + If(TypeOf activity2 Is CompensationHandlerActivity, 1, 0))
            Next
            If (num > 1) Then
                errors.Add(New ValidationError(SR.GetString("Error_MoreThanOneCancelHandler", New Object() { activity.GetType.Name }), &H527))
            End If
            If (num2 > 1) Then
                errors.Add(New ValidationError(SR.GetString("Error_MoreThanOneFaultHandlersActivityDecl", New Object() { activity.GetType.Name }), &H52A))
            End If
            If (num3 > 1) Then
                errors.Add(New ValidationError(SR.GetString("Error_MoreThanOneCompensationDecl", New Object() { activity.GetType.Name }), &H52B))
            End If
            If manager.ValidateChildActivities Then
                Dim activity3 As Activity
                For Each activity3 In Helpers.GetAllEnabledActivities(activity)
                    errors.AddRange(ValidationHelpers.ValidateActivity(manager, activity3))
                Next
            End If
            Return errors
        End Function

        Public Overrides Function ValidateActivityChange(ByVal activity As Activity, ByVal action As ActivityChangeAction) As ValidationError
            If (activity Is Nothing) Then
                Throw New ArgumentNullException("activity")
            End If
            If (action Is Nothing) Then
                Throw New ArgumentNullException("action")
            End If
            If (((activity.ExecutionStatus <> ActivityExecutionStatus.Initialized) AndAlso (activity.ExecutionStatus <> ActivityExecutionStatus.Executing)) AndAlso (activity.ExecutionStatus <> ActivityExecutionStatus.Closed)) Then
                Return New ValidationError(SR.GetString("Error_DynamicActivity", New Object() { activity.QualifiedName, Enum.GetName(GetType(ActivityExecutionStatus), activity.ExecutionStatus) }), 260)
            End If
            Return Nothing
        End Function

    End Class
End Namespace

